﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;
using LocalSettings;
using QuickTasks.ViewModels;
using Microsoft.Phone.UserData;
using System.Threading;
using QuickTasks.ViewModels.Tables;
using System.Collections.ObjectModel;
namespace QuickTasks
{

    public class FavoritePeople
    {
       
        #region Events

     
  

        public void GetPhoneNumbers(string id, string type)
        {
            string phoneNumber = "";
            string name = "";
            try
            {
                using(QuickTaskDataContext context=new QuickTaskDataContext(App.ConnectionString))
                {
                    var xqueryfavlist = from i in context.people where i.id.ToString() == id select i;
                    foreach (var objlist in xqueryfavlist)
                    {
                        if(type=="mobile")
                            phoneNumber = objlist.mobile;
                        if (type == "home")
                            phoneNumber = objlist.home;
                        if (type == "mwork")
                            phoneNumber = objlist.mwork;
                        name = objlist.name;
                        break;
                    }
                    PhoneCallTask phoneCallTask = new PhoneCallTask();
                    phoneCallTask.PhoneNumber = phoneNumber;
                    phoneCallTask.DisplayName = name;
                    phoneCallTask.Show();
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in GetPhoneNumbers method In FavoritePeople file", ex);
            }
        }

       
        public List<FavoritePeopleProp> LoadFavorites()
        {
            List<FavoritePeopleProp> objFavList = new List<FavoritePeopleProp>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var FavEmails = from i in context.people select i;
                    foreach (var email in FavEmails)
                    {
                        FavoritePeopleProp objFav = new FavoritePeopleProp();
                        objFav.Name = email.name;
                        objFav.Mobile = email.mobile;
                        objFav.mWork = email.mwork;
                        objFav.Home = email.home;
                        objFav.Personal = email.personal;
                        objFav.work = email.work;
                        objFav.other = email.other;
                        if (objFav.Mobile == null)
                        {
                            objFav.mvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.mtype = "Mobile";
                            objFav.mtype1 = email.name + "(mobile)";
                            objFav.mvis = Visibility.Visible;
                        }
                        if (objFav.mWork == null)
                        {
                            objFav.mwvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.mwtype = "Work";
                            objFav.mwtype1 = email.name + "(mwork)";
                            objFav.mwvis = Visibility.Visible;
                        }
                        if (objFav.Home == null)
                        {
                            objFav.hvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.htype = "Home";
                            objFav.htype1 = email.name + "(home)";
                            objFav.hvis = Visibility.Visible;
                        }
                        if (objFav.Personal == null)
                        {
                            objFav.pvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.ptype = "Personal";
                            objFav.ptype1 = email.name + "(Personal)";
                            objFav.pvis = Visibility.Visible;
                        }
                        if (objFav.work == null)
                        {
                            objFav.wvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.wtype = "Work";
                            objFav.wtype1 = email.name + "(Work)";
                            objFav.wvis = Visibility.Visible;
                        }
                        if (objFav.other == null)
                            objFav.ovis = Visibility.Collapsed;
                        else
                        {
                            objFav.otype = "Other";
                            objFav.otype1 = email.name + "(Other)";
                            objFav.ovis = Visibility.Visible;
                        }
                        objFavList.Add(objFav);
                    }
                }
               
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in LoadFavorites method In FavoritePeople file", ex);
          
            }
            return objFavList;
        }


        public ObservableCollection<FavoritePeopleProp> LoadFavotitePeopleCities(string id)
        {

            ObservableCollection<FavoritePeopleProp> objFavList = new ObservableCollection<FavoritePeopleProp>();
             using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                var FavEmails = from i in context.people where i.place== id select i;
                foreach (var objlist in FavEmails)
                {
                    FavoritePeopleProp objfav = new FavoritePeopleProp();
                    if (objlist.id != null) objfav.Id = objlist.id.ToString();
                    if (objlist.name != null) objfav.Name = objlist.name;
                    
                    if (objlist.place != null) objfav.DefaultPlace = objlist.place;

                    if (objlist.mobile != null)
                    {
                        objfav.mobileVisibility = Visibility.Visible;
                        objfav.Mobile = objlist.mobile;
                    }
                    else
                        objfav.mobileVisibility = Visibility.Collapsed;
                    if (objlist.home != null)
                    {
                        objfav.HomeVisibility = Visibility.Visible;
                        objfav.Home = objlist.home;
                    }
                    else
                        objfav.HomeVisibility = Visibility.Collapsed;
                    if (objlist.mwork != null)
                    {
                        objfav.WorkVisibility = Visibility.Visible;
                        objfav.mWork = objlist.mwork;
                    }
                    else
                        objfav.WorkVisibility = Visibility.Collapsed;
                 
                    objFavList.Add(objfav);
                }
            }
            return objFavList;
        }
        
       
        #endregion
    }
}


